using hao_Common.Http.Interfaces;
using Microsoft.Extensions.Logging;

namespace hao_Common.Http.Examples
{
    /// <summary>
    /// HTTP助手使用示例
    /// </summary>
    public class HttpHelperExample
    {
        private readonly IHttpHelper _httpHelper;
        private readonly ILogger<HttpHelperExample> _logger;

        public HttpHelperExample(IHttpHelper httpHelper, ILogger<HttpHelperExample> logger)
        {
            _httpHelper = httpHelper;
            _logger = logger;
        }

        /// <summary>
        /// GET请求示例
        /// </summary>
        public async Task<UserInfo?> GetUserAsync(int userId)
        {
            try
            {
                var url = $"https://jsonplaceholder.typicode.com/users/{userId}";
                return await _httpHelper.GetAsync<UserInfo>(url);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取用户信息失败: {UserId}", userId);
                return null;
            }
        }

        /// <summary>
        /// POST请求示例
        /// </summary>
        public async Task<PostInfo?> CreatePostAsync(CreatePostRequest request)
        {
            try
            {
                var url = "https://jsonplaceholder.typicode.com/posts";
                return await _httpHelper.PostAsync<PostInfo>(url, request);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "创建文章失败");
                return null;
            }
        }

        /// <summary>
        /// 带请求头的GET请求示例
        /// </summary>
        public async Task<string> GetWithHeadersAsync()
        {
            try
            {
                var headers = new Dictionary<string, string>
                {
                    ["Authorization"] = "Bearer your-token-here",
                    ["X-API-Key"] = "your-api-key"
                };

                var url = "https://httpbin.org/headers";
                return await _httpHelper.GetStringAsync(url, headers);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "带请求头的GET请求失败");
                return string.Empty;
            }
        }

        /// <summary>
        /// 表单数据提交示例
        /// </summary>
        public async Task<string> PostFormAsync()
        {
            try
            {
                var formData = new Dictionary<string, string>
                {
                    ["name"] = "张三",
                    ["email"] = "zhangsan@example.com",
                    ["message"] = "这是一条测试消息"
                };

                var url = "https://httpbin.org/post";
                return await _httpHelper.PostStringAsync(url, formData);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "表单提交失败");
                return string.Empty;
            }
        }

        /// <summary>
        /// 文件下载示例
        /// </summary>
        public async Task<bool> DownloadFileAsync()
        {
            try
            {
                var url = "https://httpbin.org/json";
                var savePath = Path.Combine(Path.GetTempPath(), "downloaded_file.json");
                
                return await _httpHelper.DownloadFileAsync(url, savePath);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "文件下载失败");
                return false;
            }
        }
    }

    /// <summary>
    /// 用户信息模型
    /// </summary>
    public class UserInfo
    {
        public int Id { get; set; }
        public string Name { get; set; } = string.Empty;
        public string Email { get; set; } = string.Empty;
        public string Phone { get; set; } = string.Empty;
        public string Website { get; set; } = string.Empty;
    }

    /// <summary>
    /// 文章信息模型
    /// </summary>
    public class PostInfo
    {
        public int Id { get; set; }
        public int UserId { get; set; }
        public string Title { get; set; } = string.Empty;
        public string Body { get; set; } = string.Empty;
    }

    /// <summary>
    /// 创建文章请求模型
    /// </summary>
    public class CreatePostRequest
    {
        public int UserId { get; set; }
        public string Title { get; set; } = string.Empty;
        public string Body { get; set; } = string.Empty;
    }
}